package com.dev;
import android.graphics.Point;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.view.Gravity;
public class QuerySettingsForCEditor extends LinearLayout implements OnClickListener,OnLongClickListener {
	WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
	MainActivity ma;
	CheckableItem ciShowSpace;
	CheckableItem ciShowTab;
	public QuerySettingsForCEditor(MainActivity ma){
		super(ma);
		this.ma=ma;
		setOrientation(VERTICAL);
		setBackground(new GUI.BorderDrawable());
		wlp.gravity=Gravity.LEFT|Gravity.TOP;
		wlp.flags=wlp.FLAG_LAYOUT_IN_SCREEN;
		ciShowSpace=new CheckableItem(ma,"显示空格字符");
		ciShowSpace.setOnClickListener(this);
		ciShowTab=new CheckableItem(ma,"显示tab字符");
		ciShowTab.setOnClickListener(this);
		LinearLayout.LayoutParams itlp=new LinearLayout.LayoutParams(-1,ma.dialogTitleBarHeight);
		addView(ciShowSpace,itlp);
		addView(ciShowTab,itlp);
	}
	CEditorTab cet;
	public void show(CEditorTab cet,View anchor){
		this.cet=cet;
		Rect rect=new Rect();
		anchor.getGlobalVisibleRect(rect);
		wlp.x=rect.left;
		wlp.y=rect.bottom;
		wlp.width=ma.wp/2;
		wlp.height=-2;
		CIndentEditor cie=cet.cep.cie;
		ciShowSpace.setSelected(cie.showSpaceChar);
		ciShowTab.setSelected(cie.showTabChar);
		ma.wm.addView(this,wlp);
	}
	void dismiss(){
		ma.wm.removeView(this);
	}
	@Override
	public void onClick(View view) {
		if(view==ciShowSpace){
			CIndentEditor cie=cet.cep.cie;
			cie.showSpaceChar=true;
			cie.invalidate();
		}else if(view==ciShowTab){
			CIndentEditor cie=cet.cep.cie;
			cie.showTabChar=true;
			cie.invalidate();
		}
	}
	@Override
	public boolean onLongClick(View view) {
		return true;
	}
	Point touchDown=new Point();
	@Override
	public boolean onTouchEvent(MotionEvent event){
		int x=(int)event.getX();
		int y=(int)event.getY();
		switch(event.getAction()){
			case MotionEvent.ACTION_DOWN:
				touchDown.x=x;
				touchDown.y=y;
				break;
			case MotionEvent.ACTION_UP:
				Rect visible=new Rect();
				getGlobalVisibleRect(visible);
				int[] is=new int[2];
				getLocationOnScreen(is);
				if(touchDown.x>=visible.right||touchDown.x<visible.right||touchDown.y>=visible.bottom||touchDown.y<visible.top){
					dismiss();
				}
		}
		super.onTouchEvent(event);
		return true;
	}
}
